package com.hulu.yec.stu.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hulu.yec.common.PageModel;
import com.hulu.yec.common.Constant;
import com.hulu.yec.stu.dao.ExamRecordDao;
import com.hulu.yec.stu.entity.ExamRecordEntity;

/**
 * Servlet implementation class examRecordServlet
 */
@WebServlet("/stu/ExamRecordServlet")
public class ExamRecordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	ExamRecordDao examRecordDao = new ExamRecordDao();
   
	protected void doGet(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}

	
	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		  //解决POST请求的中文乱码问题
		  request.setCharacterEncoding("UTF-8");
		  String command = request.getParameter("command");
				if("list".equals(command)){
					  list(request,response);
				  }
			    }


	private void list(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		   request.setCharacterEncoding("UTF-8");
		   System.out.println("isPass: " + request.getParameter("isPass"));
		   int pageNo = Constant.DEFAULT_PAGE_NO;
		   int pageSize = Constant.DEFAULT_PAGE_SIZE;
		   String no = request.getParameter("pageNo");
		   String size = request.getParameter("pageSize");
		   
		   if(no != null){
			   pageNo = Integer.parseInt(no);
		   }
		   if(size != null){
			   pageSize = Integer.parseInt(size);
		   }
		   ExamRecordEntity  entity = new ExamRecordEntity();
		   String isPass = request.getParameter("isPass");
		   String paperName = request.getParameter("paperName");
		   if(isPass !=null){
			   String str = request.getParameter("isPass");
			   request.setAttribute("isPass", isPass);
			   if(str.equals("通过")){
				   int nowisPass = 1;
				   entity.setIsPass(nowisPass);
			   }else if (str.equals("未通过")){
				   int nowisPass = 2;
				   entity.setIsPass(nowisPass);
			   }else if (str.equals("超时未提交")){
				   int nowisPass = 3;
				   entity.setIsPass(nowisPass);
		       } 
				   else if (str.equals("1")){
				   int nowisPass = 1; 
				   entity.setIsPass(nowisPass);
			   }else if (str.equals("2")){
				   int nowisPass = 2;
				   entity.setIsPass(nowisPass);
			   }else if (str.equals("3")){
				   int nowisPass = 3;
				   entity.setIsPass(nowisPass);
			   }
		   }
		   if(paperName !=null && !paperName.equals(" ")){
			   String nowpaperName = request.getParameter("paperName");
			   entity.setPaperName(paperName);
			   request.setAttribute("paperName", paperName);
		   }
		   try {
				PageModel<ExamRecordEntity> exModel = ExamRecordDao.list(entity, pageNo,
						pageSize);
				request.setAttribute("exModel", exModel);
				request.setAttribute("entity", entity);

				request.getRequestDispatcher("/WEB-INF/view/stu/sturecord.jsp").forward(request, response);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}
